import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import(/* webpackChunkName: "home" */ '../views/home.vue'),
    children: [{
      path: '/users',
      name: 'users',
      component: () => import(/* webpackChunkName: "users" */ '../views/users.vue')
    },
    {
      path: '/roles',
      name: 'roles',
      component: () => import(/* webpackChunkName: "roles" */ '../views/roles.vue')
    },
    {
      path: '/rights',
      name: 'rights',
      component: () => import(/* webpackChunkName: "rights" */ '../views/rights.vue')
    },{
      path: '/categories',
      name: 'categories',
      component: () => import(/* webpackChunkName: "categories" */ '../views/categories.vue')
    },{
      path: '/goods',
      name: 'goods',
      component: () => import(/* webpackChunkName: "goods" */ '../views/goods.vue')
    },{
      path: '/params',
      name: 'params',
      component: () => import(/* webpackChunkName: "params" */ '../views/params.vue')
    },{
      path: '/orders',
      name: 'orders',
      component: () => import(/* webpackChunkName: "orders" */ '../views/orders.vue')
    }
  ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import(/* webpackChunkName: "home" */ '../views/home.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  //未登录跳去登录
  if (to.path !== '/login') {
    if (!localStorage.getItem('userinfo')) {
      next('login')
    }
    //登录了禁止去登录页面
  } else if (to.path == '/login') {
    if (localStorage.getItem('userinfo')) {
      next(from.path)
    }
  }
  // 如果用户未能验证身份，则 `next` 会被调用两次
  next()
})

export default router
